#coding=utf-8
from selenium import webdriver
from selenium.webdriver.support.ui import Select
import time
import traceback
import sys
reload(sys)
sys.setdefaultencoding('utf8')

# web manual: http://selenium-python.readthedocs.org/en/latest/

if __name__ == '__main__':
	url = 'http://www.dilidili.com/anime/onepiece/'
	browser = webdriver.Chrome()
	browser.get(url)
	browser.implicitly_wait(5)
	
	total_content = ''
	
	for j in range(3,0,-1):
		browser.find_element_by_xpath('//div[@class="main"]/div/ul/li[' + str(j) + ']').click()
		series_list = browser.find_elements_by_xpath('//div[@class="main0"]/ul[' + str(j) + ']/table/tbody/tr')
		for i in range(len(series_list),0,-1):
			try:
				series_id = browser.find_element_by_xpath('//div[@class="main0"]/ul[' + str(j) + ']/table/tbody/tr[' + str(i) + ']/td[1]').text
				series_name = browser.find_element_by_xpath('//div[@class="main0"]/ul[' + str(j) + ']/table/tbody/tr[' + str(i) + ']/td[2]/a').text
				series_date = browser.find_element_by_xpath('//div[@class="main0"]/ul[' + str(j) + ']/table/tbody/tr[' + str(i) + ']/td[3]').text
				
				print 'id:' + series_id + '\tname:' + series_name + '\tdate:' + series_date
				total_content = total_content + series_id + ',' + series_name + ',' + series_date + '\n'
			except:
				pass
	
	with open('op-series.csv','w') as f:
		# if not change encode, file encoding will be messy
		f.write(total_content.decode('utf-8').encode('gbk'))
	browser.quit()